package com.cnswhy.cloud.server.core.channel.verify.service.impl;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.server.core.channel.verify.model.IPinfoResponse;
import com.cnswhy.cloud.server.core.channel.verify.service.AliyunIpService;
import com.cnswhy.cloud.server.core.channel.verify.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class AliyunIpServiceImpl implements AliyunIpService {

    @Override
    public IPinfoResponse getIPinfo(String ip) {
        String host = "https://ipquery.market.alicloudapi.com";
        String path = "/query";
        String method = "GET";
        String appcode = "3ac78dc0a3264ff98dde6e22121cbdbd";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("ip", ip);
        IPinfoResponse iPinfoResponse = null;
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            String responseMsg = new String(EntityUtils.toString(response.getEntity()));
            System.out.println(responseMsg);
            iPinfoResponse = JSON.parseObject(responseMsg, IPinfoResponse.class);

            if (iPinfoResponse == null) {
                iPinfoResponse = new IPinfoResponse();
                iPinfoResponse.setRet("40001");
                iPinfoResponse.setMsg("网络异常");
            }
            if (!iPinfoResponse.getRet().equals("200")) {
                if (StringUtils.isEmpty(iPinfoResponse.getMsg())) {
                    iPinfoResponse.setMsg("网络异常");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("exit verify4:{}", JSON.toJSONString(iPinfoResponse));
        return iPinfoResponse;
    }

/*
    public IPinfoResponse getIPinfo(String ip) {
        log.info("enter info getIPinfo:{}", ip);
        //String host = "http://api01.aliyun.venuscn.com";
        String host = "https://ipquery.market.alicloudapi.com/query";
        String path = "/ip";
        String method = "GET";
        //String appcode = "4cdedb8fa200492a9eef946ff64fc8e7";
        String appcode = "3ac78dc0a3264ff98dde6e22121cbdbd";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("ip", ip);
        IPinfoResponse iPinfoResponse =  null;
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body BwRCJaY2QwAFDkSG5twZhJkp3FQNjr

            String responseMsg = new String(EntityUtils.toString(response.getEntity()));
            log.info(responseMsg);
            System.out.println(response);
            iPinfoResponse = JSON.parseObject(responseMsg, IPinfoResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(iPinfoResponse == null){
            iPinfoResponse = new IPinfoResponse();
            iPinfoResponse.setRet("40001");
            iPinfoResponse.setMsg("网络异常");
        }
        if(!iPinfoResponse.getRet().equals("200")){
            if(StringUtils.isEmpty(iPinfoResponse.getMsg())){
                iPinfoResponse.setMsg("网络异常");
            }
        }
        log.info("exit verify4:{}", FieldHelper.getNotNullObj(iPinfoResponse));
        return iPinfoResponse;
    }*/
}
